package com.example.demo9.servlet;

import com.example.demo9.mapper.UserMapper;
import com.example.demo9.pojo.User;
import com.example.demo9.utils.MybatisUtils;
import com.google.gson.Gson;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "use",value = "/user")
public class UserSevrlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if (method.equals("showDesc")) {
            showDesc(req, resp);

        }

    }

    private void showDesc(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String names = req.getParameter("names");
        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

      User user=  mapper.Usedesc(names);
        // 处理 list 数据...
        HashMap<String,Object> map=new HashMap<>();
        if (user!=null) {
            map.put("code",200);
            map.put("msg","查询成功");
            map.put("rows",user);

        }else {
            map.put("code",500);
            map.put("msg","查询失败");
            map.put("rows",user);
        }
      PrintWriter out=  resp.getWriter();
      out.append(new Gson().toJson(map));

    }
}
